import dayjs from 'dayjs'

// 日历配置项
const minDate = Date.now()
export const calendarConfig = {
  // 日期格式化
  formatter(day) {
    const date = new Date(day.date)
    const now = new Date()
    const year = date.getFullYear()
    const month = date.getMonth()
    const da = date.getDate()
    const nowYear = now.getFullYear()
    const nowMonth = now.getMonth()
    const nowDa = now.getDate()

    if (year === nowYear && month === nowMonth && da === nowDa) {
      day.topInfo = '今天'
    }

    if (day.type === 'start') {
      day.bottomInfo = '入住'
    }

    if (day.type === 'end') {
      day.bottomInfo = '离开'
    }

    if (day.type === 'same') {
      day.bottomInfo = '入住/离开'
    }

    return day
  },
  minDate,
  maxDate: minDate + 3600 * 1000 * 24 * 30,
  title: '日期选择',
  innerDisplayFormat(value, rangeType) {
    if (!value) {
      return rangeType === 'start' ? '入住日期' : '离店日期'
    }

    return dayjs(value).format('YYYY年MM月DD日')
  },
}

/**
 * 计算间夜信息
 * @param startDate 开始日期
 * @param endDate 结束日期
 * @return number
 */
export function getRoomNight(startDate, endDate) {
  return dayjs(endDate).diff(dayjs(startDate), 'day')
}

/**
 * 日期转文字
 * @param date
 * @return string
 */
export function dateTransformText(date) {
  const roomNight = getRoomNight(dayjs(Date.now()).format('YYYY-MM-DD 00:00:00'), new Date(date))
  if (roomNight === 0) {
    return '今天'
  } else if (roomNight === 1) {
    return '明天'
  } else {
    const day = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
    return day[dayjs(date).day()]
  }
}
